using System;
using System.CodeDom.Compiler;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Markup;
using Common;
using Microsoft.Win32;
using SExperiment;

namespace MainView.StandardCurve
{
	public partial class StandardCurveManageWindow : Window, IComponentConnector
	{
		private StandardCurveManage _StandardCurveManage = new StandardCurveManage();

		public StandardCurveManageWindow()
		{
			_StandardCurveManage.LoadAllStandardCurve();
			InitializeComponent();
			listBoxStandardCurve.ItemsSource = _StandardCurveManage.Items;
			listBoxStandardCurve.SelectedIndex = 0;
		}

		private void btnOk_Click(object sender, RoutedEventArgs e)
		{
			Close();
		}

		private void btnImport_Click(object sender, RoutedEventArgs e)
		{
			ProjectStdCurve projectStdCurve = null;
			Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
			openFileDialog.Filter = string.Format("{0}{1}|*.{2}", Experiment.FileStdExt, Utility.GetMessage("File"), Experiment.FileStdExt);
			openFileDialog.FileName = string.Empty;
			openFileDialog.FilterIndex = 1;
			openFileDialog.DefaultExt = Experiment.FileStdExt;
			openFileDialog.Multiselect = true;
			if (openFileDialog.ShowDialog() == true)
			{
				string[] fileNames = openFileDialog.FileNames;
				foreach (string text in fileNames)
				{
					ProjectStdCurve projectStdCurve2 = new ProjectStdCurve();
					FileStream fileStream = null;
					StreamEx streamEx = new StreamEx();
					try
					{
						fileStream = new FileStream(text, FileMode.Open, FileAccess.Read);
						fileStream.CopyTo(streamEx);
						streamEx.Seek(0L, SeekOrigin.Begin);
						if (projectStdCurve2.ReadFromStream(streamEx) && !string.IsNullOrEmpty(projectStdCurve2.ProjectName))
						{
							string text2 = Utility.GetStandardPath() + Path.GetFileName(text);
							File.Copy(text, text2);
							projectStdCurve2.External = true;
							projectStdCurve2.Path = text2;
							_StandardCurveManage.Items.Add(projectStdCurve2);
							if (projectStdCurve == null)
							{
								projectStdCurve = projectStdCurve2;
							}
						}
						else
						{
							StringBuilder stringBuilder = new StringBuilder();
							stringBuilder.Append(Utility.GetMessage("msg_not_standardcurve_file"));
							stringBuilder.AppendLine(text);
							System.Windows.MessageBox.Show(stringBuilder.ToString());
						}
					}
					catch (Exception)
					{
					}
					finally
					{
						if (streamEx != null)
						{
							streamEx.Close();
						}
						if (fileStream != null)
						{
							fileStream.Close();
						}
					}
				}
			}
			if (projectStdCurve != null)
			{
				listBoxStandardCurve.SelectedItem = projectStdCurve;
			}
		}

		private void btnExport_Click(object sender, RoutedEventArgs e)
		{
			ProjectStdCurve projectStdCurve = listBoxStandardCurve.SelectedItem as ProjectStdCurve;
			if (projectStdCurve != null)
			{
				System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog();
				saveFileDialog.Filter = string.Format("{0}{1}|*.{2}", Experiment.FileStdExt, Utility.GetMessage("File"), Experiment.FileStdExt);
				saveFileDialog.FileName = Path.GetFileName(projectStdCurve.Path);
				saveFileDialog.FilterIndex = 1;
				saveFileDialog.InitialDirectory = Utility.GetMyDocumentFolder();
				if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
				{
					projectStdCurve.Save(saveFileDialog.FileName);
					System.Windows.MessageBox.Show(Utility.GetMessage("msg_standard_expert_success"));
				}
			}
		}

		private void btnDelete_Click(object sender, RoutedEventArgs e)
		{
			ProjectStdCurve projectStdCurve = listBoxStandardCurve.SelectedItem as ProjectStdCurve;
			if (projectStdCurve != null && System.Windows.MessageBox.Show(Utility.GetMessage("msg_delete_standardcurve_file_question"), Utility.GetMessage("Warning"), MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes)
			{
				File.Delete(projectStdCurve.Path);
				int num = listBoxStandardCurve.SelectedIndex;
				_StandardCurveManage.Items.Remove(projectStdCurve);
				if (num >= _StandardCurveManage.Items.Count)
				{
					num = _StandardCurveManage.Items.Count - 1;
				}
				listBoxStandardCurve.SelectedIndex = num;
			}
		}

		[GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
		[DebuggerNonUserCode]
		internal Delegate _CreateDelegate(Type delegateType, string handler)
		{
			return Delegate.CreateDelegate(delegateType, this, handler);
		}
	}
}
